using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;

namespace ImageReview
{
    public partial class PreviewTable : System.Web.UI.UserControl
    {
		private List<TPDSControl.ImageSet> _imagesets = new List<TPDSControl.ImageSet>();

		public List<TPDSControl.ImageSet> Imagesets
		{
			get
			{
				return _imagesets;
			}
			set
			{
				_imagesets = value;
				ShowImageSets();
			}
		}
        
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private void ShowImageSets( )
		{
			PlaceHolder PreviewTablePlaceHolder = (PlaceHolder)this.FindControl("PreviewTablePlaceHolder");
            
            // First clear all current controls.
            PreviewTablePlaceHolder.Controls.Clear( );


			if ((Imagesets != null) && (Imagesets.Count > 0))
			{
				AddHeaderRow(PreviewTablePlaceHolder);

                // Add the controls for each image set, passing in the set count to create unique ids.
                uint setcount = 1;
                foreach (TPDSControl.ImageSet imageset in Imagesets)
				{
                    LoadImageSet( imageset.Filepath, PreviewTablePlaceHolder, setcount );
                    setcount++;
				}
			}
			else
			{
				Label lblNoImages = (Label)this.FindControl("lblNoImages");
				lblNoImages.Visible = true;
			}
		}

        private void LoadImageSet( string filepath, PlaceHolder previewRowsHolder, uint setcount )
        {
            try
            {
                TPDSControl.ImageSet imageset = TPDSControl.ImageSet.LoadOne(filepath);
                if( imageset != null )
                    AddPreviewRows( imageset, previewRowsHolder, setcount );
            }
            catch (Exception ex)
            {
                TPDSConfig.ErrorLog.WriteError(ex.Message, filepath);
            }
        }

        private void AddPreviewRows( TPDSControl.ImageSet imageset, PlaceHolder previewRowsHolder, uint setcount )
        {
            PreviewRow previewRow = null;

            // Counter to give each table a unique id.
            int rowcount = 1;

            foreach (TPDSControl.Image image in imageset.Images)
            {
                if( ( (CurrentPage)Session[ "CurrentPage" ] == CurrentPage.Review && image.IsWaiting == true ) || ( (CurrentPage)Session[ "CurrentPage" ] == CurrentPage.Accept && image.IsWaiting == false ) )
                    continue;

                if( image.IsBarcode || previewRow == null )
                {
                    if( previewRow != null )
                        previewRowsHolder.Controls.Add( previewRow );
                    
                    // Create PreviewRow object.
                    previewRow = (PreviewRow)LoadControl("PreviewRow.ascx");
                    previewRow.ImageSet = imageset;
                    previewRow.ID = String.Format( "PreviewRow-{0:d2}-{1:d2}", setcount, rowcount );
                    rowcount++;
                }

                // Assign image object to row
                previewRow.RowImages.Add( image );
            }

            if( previewRow != null )
                previewRowsHolder.Controls.Add( previewRow );
        }


        private void AddHeaderRow(PlaceHolder previewRowsHolder)
        {
            Control previewHeaderRow1;
            previewHeaderRow1 = LoadControl("PreviewHeader.ascx");
            previewRowsHolder.Controls.Add(previewHeaderRow1);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {

            string nextPage = ResolveClientUrl("~/Process.aspx");
            Response.Redirect(nextPage, false);
        }
    }
}